﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace TS_XY_Train_Repaire.Locomotive.TrainRepair.Work.Fittings
{
    public partial class Locomotive_TrainRepair_Work_Fittings_UpDownTrai_Info_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Locomotive_TrainRepair_Work_Fittings_UpDownTrai_Info_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_TrainRepair_Work_Fittings_UpDownTrai_Info_ManaForm_Load(object sender, EventArgs e)
        {
            InitGrid();
        }

        private void Locomotive_TrainRepair_Work_Fittings_UpDownTrai_Info_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void InitGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,f_itname,f_id,f_fid,f_name as f_tdname,f_tdid,f_ftid,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_TM_Loco_Fittings_Work.f_ftid) as f_ftname,f_code,f_plname,f_depid,(select f_name from TS_S_T_P_Department where f_id=TS_TR_TM_Loco_Fittings_Work.f_depid) as f_depname,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_TM_Loco_Fittings_Work.f_mapid) as f_mapname,f_type,(case f_type when 0 then '下车' else '上车' end) as f_typename,f_state,(case f_fid when -1 then ' ' else (select CONVERT(varchar(100), f_indate, 23) from TS_TR_B_Loco_Fittings_Base_List where f_id=TS_TR_TM_Loco_Fittings_Work.f_fid) end ) as f_indate from TS_TR_TM_Loco_Fittings_Work where f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + " and f_state in (0,8) and f_del=0";
            DataTable _tb = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _tb);
            gc_Main.DataSource = _tb;
            gc_Main.Refresh();
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            try
            {
                if (gv_Main.SelectedRowsCount > 0)
                {
                    TS_Win_Class.FillListTypeAct(cb_ftype, "select f_id,f_name from TS_S_T_P_TrainDepSys_Type where f_tdid=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tdid").ToString());
                    try
                    {
                        cb_ftype.SelectedValue = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_ftid").ToString();
                    }
                    catch
                    {
                    }
                    Thread.Sleep(10);
                    try
                    {
                        cb_fcode.SelectedValue = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_fid").ToString();
                    }
                    catch
                    {
                    }
                    te_place.Tag = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_mapid").ToString();
                    te_place.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_mapname").ToString();
                    te_plName.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_plname").ToString();
                }
            }
            catch
            {
            }
        }

        private void te_place_DoubleClick(object sender, EventArgs e)
        {
            UserCon.ucPub.Map.uc_Pub_Map_Select_ListView_ByOne_CallBack uc = new UserCon.ucPub.Map.uc_Pub_Map_Select_ListView_ByOne_CallBack();
            uc._LoadType = "1";
            uc.Tag = te_place;
            if (uc.ShowDialog() == DialogResult.OK)
            {
            }
            else
            {
            }
        }

        private void cb_ftype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_ftype.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_fcode, "select f_id,f_code as f_name from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + cb_ftype.SelectedValue.ToString() + " and f_del=0");
                cb_fcode.Refresh();
                if (cb_fcode.Items.Count > 0)
                {
                    cb_fcode.SelectedIndex = 0;
                }
            }
            else
            {
                cb_fcode.DataSource = null;
                cb_fcode.Refresh();
            }
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            if (cb_ftype.SelectedIndex > -1 && cb_fcode.SelectedIndex > -1 && te_place.Tag.ToString().Trim() != "-1" && te_plName.Text.Trim().Length > 0 && gv_Main.SelectedRowsCount > 0)
            {
                string strSQL = " update TS_TR_TM_Loco_Fittings_Work set f_ftid=" + cb_ftype.SelectedValue.ToString() + ",f_mapid=" + te_place.Tag.ToString() + ",f_plname='" + te_plName.Text.Trim() + "',f_fid=" + cb_fcode.SelectedValue.ToString() + ",f_code='" + cb_fcode.Text.Trim().ToString() + "' where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
                TS_Win_Class.MySqlCom(strSQL);
                InitGrid();
            }
            else
            {
                MessageBox.Show("请完整填写数据！");
            }
        }

        private void bt_New_Click(object sender, EventArgs e)
        {
            if (cb_ftype.SelectedIndex > -1)
            {
                TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_DownTrain_AddNewCode _uc = new TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_DownTrain_AddNewCode();
                _uc._TDID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tdid").ToString();
                _uc._FTID = cb_ftype.SelectedValue.ToString();
                _uc._FTName = cb_ftype.Text.ToString();
                _uc._State = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_type").ToString();
                if (_uc.ShowDialog() == DialogResult.OK)
                {
                    TS_Win_Class.FillListTypeAct(cb_fcode, "select f_id,f_code as f_name from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + cb_ftype.SelectedValue.ToString() + " and f_del=0");
                    cb_fcode.Refresh();
                    if (cb_fcode.Items.Count > 0)
                    {
                        cb_fcode.SelectedIndex = 0;
                    }
                }
                else
                {
                }
            }
            else
            {
                MessageBox.Show("请先选择一个有效的配件型号！");
            }
        }

        private void bt_Reload_Wait_Click(object sender, EventArgs e)
        {
            InitGrid_Wait();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,f_id,f_fid,f_itname,f_name as f_tdname,f_tdid,f_ftid,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_TM_Loco_Fittings_Work.f_ftid) as f_ftname,f_code,f_plname,f_depid,(select f_name from TS_S_T_P_Department where f_id=TS_TR_TM_Loco_Fittings_Work.f_depid) as f_depname,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_TM_Loco_Fittings_Work.f_mapid) as f_mapname,f_type,(case f_type when 0 then '下车' else '上车' end) as f_typename,f_state,(case f_fid when -1 then ' ' else (select CONVERT(varchar(100), f_indate, 23) from TS_TR_B_Loco_Fittings_Base_List where f_id=TS_TR_TM_Loco_Fittings_Work.f_fid) end ) as f_indate from TS_TR_TM_Loco_Fittings_Work where datediff(day,f_itdate,'" + de_Date.Text.Trim() + "')=0 and f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + " and f_state in (9) and f_del=0";
            DataTable _tb = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _tb);
            gc_Has.DataSource = _tb;
            gc_Has.Refresh();
        }
        private void InitGrid_Wait()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,f_id,f_fid,f_itname,f_name as f_tdname,f_tdid,f_ftid,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_TM_Loco_Fittings_Work.f_ftid) as f_ftname,f_code,f_plname,f_depid,(select f_name from TS_S_T_P_Department where f_id=TS_TR_TM_Loco_Fittings_Work.f_depid) as f_depname,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_TM_Loco_Fittings_Work.f_mapid) as f_mapname,f_type,(case f_type when 0 then '下车' else '上车' end) as f_typename,f_state,(case f_fid when -1 then ' ' else (select CONVERT(varchar(100), f_indate, 23) from TS_TR_B_Loco_Fittings_Base_List where f_id=TS_TR_TM_Loco_Fittings_Work.f_fid) end ) as f_indate from TS_TR_TM_Loco_Fittings_Work where f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + " and f_state not in (0,8,9) and f_del=0";
            DataTable _tb = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _tb);
            gc_Wait.DataSource = _tb;
            gc_Wait.Refresh();
        }

        private void cms_Main_Opening(object sender, CancelEventArgs e)
        {
            cms_Main_Send.Visible = false;
            cms_Main_Back.Visible = false;
            try
            {
                if (cms_Main.SourceControl.Name == "gc_Main")
                {
                    if (gv_Main.SelectedRowsCount > 0)
                    {
                        gv_Main.GetRowCellValue(0, "f_id").ToString();
                        cms_Main_Send.Visible = true;
                    }
                    else
                    {
                        cms_Main_Send.Visible = false;
                    }
                }
                if (cms_Main.SourceControl.Name == "gc_Wait")
                {
                    if (gv_Wait.SelectedRowsCount > 0)
                    {
                        gv_Wait.GetRowCellValue(0, "f_id").ToString();
                        cms_Main_Back.Visible = true;
                    }
                    else
                    {
                        cms_Main_Back.Visible = false;
                    }
                }
            }
            catch
            {
            }
        }

        private void cms_Main_Send_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("选中的配件将进入审核流程，确定吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                string str = "";
                foreach (int iii in gv_Main.GetSelectedRows())
                {
                    str += gv_Main.GetRowCellValue(iii, "f_id").ToString() + ",";
                }
                str = str.Substring(0, str.Length - 1);
                string strSQL = "update TS_TR_TM_Loco_Fittings_Work set f_state=2,f_stuffid1=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_stime1=getdate(),f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id in ("+str+")";
                TS_Win_Class.MySqlCom(strSQL);
                InitGrid();
            }
        }

        private void cms_Main_Back_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("选中的配件将撤回审核流程重新填写，确定吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                string str = "";
                foreach (int iii in gv_Wait.GetSelectedRows())
                {
                    str += gv_Wait.GetRowCellValue(iii, "f_id").ToString() + ",";
                }
                str = str.Substring(0, str.Length - 1);
                string strSQL = "update TS_TR_TM_Loco_Fittings_Work set f_state=0,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id in (" + str + ")";
                TS_Win_Class.MySqlCom(strSQL);
                InitGrid();
                InitGrid_Wait();
            }
        }
    }
}